package com.bz.edu.info.manager.controller;

import com.bz.edu.info.manager.service.StudentService;

import java.util.Scanner;

public class StudentController {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        studentLoop:
        while (true) {
            System.out.println("---------欢迎来到学生学籍管理系统--------------");
            System.out.println("请输入你的选择");
            System.out.println("1.增加新学生信息");
            System.out.println("2.修改学生信息");
            System.out.println("3.删除学生信息");
            System.out.println("4.查看所有学生信息");
            System.out.println("5.退出系统");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    updateStudent();
                    break;
                case "3":
                    deleteStudent();
                    break;
                case "4":
                    queryStudent();
                    break;
                case "5":
                    System.out.println("感谢你的使用");
                    break studentLoop;
                default:
                    System.out.println("你的输入有误!!!");
                    break;
            }
        }
    }

    //校验是否是已存在学号
    public static int inPutOldStuNum() {
        int index;
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入学生学号");
            int inputStuNum = sc.nextInt();
            StudentService studentService = new StudentService();
            index = studentService.getIndex(inputStuNum);
            if (index == -1) {
                System.out.println("输入学号不存在，请重新输入");
            } else {
                break;
            }
        }
        return index;
    }

    //校验是否新的学号
    public static int inPutNewStuNum() {
        int inputStuNum;
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入学生学号");
            inputStuNum = sc.nextInt();
            StudentService studentService = new StudentService();
            int index = studentService.getIndex(inputStuNum);
            if (index != -1) {
                System.out.println("输入学号已存在，请重新输入");
            } else {
                break;
            }
        }
        return inputStuNum;
    }

    //输入学生信息方法
    public static String getStudentInfo()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生姓名");
        return sc.next();
    }
    //添加学生
    public static void addStudent(){
        int stuNum = inPutNewStuNum();
        String name = getStudentInfo();
        StudentService studentService = new StudentService();
        studentService.addStudent(stuNum,name);
    }
    //查看所有学生信息
    public static void queryStudent(){
        StudentService studentService = new StudentService();
        studentService.queryStudent();
    }
    //删除学生
    public static void deleteStudent()
    {
       int index = inPutOldStuNum();
       StudentService studentService = new StudentService();
       studentService.deleteStudent(index);
    }
    public static void updateStudent()
    {
        int index = inPutOldStuNum();
        String name = getStudentInfo();
        StudentService studentService = new StudentService();
        studentService.updateStudent(index,name);
    }
}
